﻿using System.Runtime.CompilerServices;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;

namespace Shop.RemoteModel.PageModule.ModuleConfig
{
    public class SkuConfig
    {      
        /// <summary>
        /// 推荐商品的方式
        /// </summary>
        [EnumValidate("shop.page.sku.type.error", typeof(PageSkuType))]
        public PageSkuType ChioseType { get; set; }

        /// <summary>
        /// 分组ID
        /// </summary>
        [EntrustValidate("_Check")]
        public long? GroupId { get; set; }
        /// <summary>
        /// 组名
        /// </summary>
        public string GroupName { get; set; }
        /// <summary>
        /// 分类ID
        /// </summary>
        [NumValidate("shop.category.id.error", 1)]
        public long? CategoryId { get; set; }

        /// <summary>
        /// 分类层级
        /// </summary>
        [NumValidate("shop.category.level.error", 1, 3)]
        public short? Level { get; set; }

        /// <summary>
        /// 排序类型
        /// </summary>
        [EnumValidate("shop.page.order.type.error", typeof(PageSkuOrderType))]
        public PageSkuOrderType? OrderType { get; set; }

        [MethodImplAttribute(MethodImplOptions.NoOptimization)]
        private void _Check ()
        {
            if ( this.ChioseType == PageSkuType.手动输入 || this.ChioseType == PageSkuType.全部商品 )
            {
                return;
            }
            else if ( this.ChioseType == PageSkuType.商品分组 )
            {
                if ( !this.GroupId.HasValue )
                {
                    throw new ErrorException("shop.goods.group.Id.null");
                }
                else if ( this.GroupName.IsNull() )
                {
                    throw new ErrorException("shop.goods.group.name.null");
                }
            }
            else if ( this.ChioseType == PageSkuType.商品分类 )
            {
                if ( !this.CategoryId.HasValue )
                {
                    throw new ErrorException("shop.goods.category.Id.null");
                }
                else if ( !this.Level.HasValue )
                {
                    throw new ErrorException("shop.goods.category.level.null");
                }
            }
        }
    }
}
